/*
 * Copyright (c) 2002-2007, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */
package fr.paris.lutece.plugins.customisation.business;

import fr.paris.lutece.util.sql.DAOUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * This class provides Data Access methods for CustomisationPortletAvalaible objects
 */
public class CustomisationPortletAvalaibleDAO implements ICustomisationPortletAvalaibleDAO
{
    private static final String SQL_QUERY_SELECT_PORTLET_TYPE = "SELECT * FROM customisation_portlet_available WHERE id_portlet_type = ? ";
    private static final String SQL_QUERY_SELECT_PORTLET_TYPE_BY_ROLE_KEY = "SELECT * FROM customisation_portlet_available WHERE role_key = ? ";
    private static final String SQL_QUERY_SELECT_PORTLET_TYPE_ACCORDING_ROLE_KEY = "SELECT * FROM customisation_portlet_available WHERE id_portlet_type = ? AND role_key = ? ";
    private static final String SQL_QUERY_INSERT_PORTLET_TYPE_AUTHORIZED = "INSERT INTO customisation_portlet_available ( id_portlet_type, role_key )  VALUES ( ?, ? ) ";
    private static final String SQL_QUERY_DELETE_PORTLET_TYPE_AUTHORIZED = " DELETE FROM customisation_portlet_available WHERE id_portlet_type = ? AND role_key = ? ";

    /**
     * Assign portlet type
     *
     * @param strIdAuthorized the portlet type identifier authorized
     * @param strRoleKey the role key
     */
    public void insert( String strIdAuthorized, String strRoleKey )
    {
        DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_PORTLET_TYPE_AUTHORIZED );
        daoUtil.setString( 1, strIdAuthorized );
        daoUtil.setString( 2, strRoleKey );
        daoUtil.executeUpdate(  );
        daoUtil.free(  );
    }

    /**
     * Load the data of a portlet type from the database
     *
     * @param strIdPortletType the portlet type identifier
     * @return the object CustomisationPortletAvalaible initialized with the data of the database
     */
    public CustomisationPortletAvalaible load( String strIdPortletType )
    {
        DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_TYPE );
        daoUtil.setString( 1, strIdPortletType );
        daoUtil.executeQuery(  );

        CustomisationPortletAvalaible portletAvalaible = new CustomisationPortletAvalaible(  );

        while ( daoUtil.next(  ) )
        {
            portletAvalaible.setIdPortletType( daoUtil.getString( 1 ) );
            portletAvalaible.setRoleKey( daoUtil.getString( 2 ) );
        }

        daoUtil.free(  );

        return portletAvalaible;
    }

    /**
     * Select the list of portlet type available according a role key
     *
     * @param strRoleKey The role key
     * @return The portlet type's list available according a role key
     */
    public List<CustomisationPortletAvalaible> selectPortletTypeAvailableByRole( String strRoleKey )
    {
        DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_TYPE_BY_ROLE_KEY );
        daoUtil.setString( 1, strRoleKey );
        daoUtil.executeQuery(  );

        List<CustomisationPortletAvalaible> listPortletType = new ArrayList<CustomisationPortletAvalaible>(  );

        while ( daoUtil.next(  ) )
        {
            CustomisationPortletAvalaible portletTypeAvailable = new CustomisationPortletAvalaible(  );
            portletTypeAvailable.setIdPortletType( daoUtil.getString( 1 ) );
            portletTypeAvailable.setRoleKey( daoUtil.getString( 2 ) );
            listPortletType.add( portletTypeAvailable );
        }

        daoUtil.free(  );

        return listPortletType;
    }

    /**
     * Delete the portlet type on a list
     *
     * @param listPortletType a portlet type's list
     * @param strRoleKey the role key
     */
    private void delete( List<CustomisationPortletAvalaible> listPortletType, String strRoleKey )
    {
        for ( int i = 0; i < listPortletType.size(  ); i++ )
        {
            DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_PORTLET_TYPE_AUTHORIZED );
            daoUtil.setString( 1, listPortletType.get( i ).getIdPortletType(  ) );
            daoUtil.setString( 2, strRoleKey );
            daoUtil.executeUpdate(  );
            daoUtil.free(  );
        }
    }

    /**
     * Delete the portlet type's list according the role key
     *
     * @param strRoleKey the role key
     */
    public void deletePortletTypeAuthorizedByRole( String strRoleKey )
    {
        List<CustomisationPortletAvalaible> listPortletType = selectPortletTypeAvailableByRole( strRoleKey );

        delete( listPortletType, strRoleKey );
    }

    /**
     * Returns the status of a portlet type; if it's in a database or not
     *
     * @param strIdPortletType the portlet type identifier
     * @param strRoleKey the role key
     * @return if a portlet type is in the database or not
     */
    public boolean checkPortletType( String strIdPortletType, String strRoleKey )
    {
        DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET_TYPE_ACCORDING_ROLE_KEY );
        daoUtil.setString( 1, strIdPortletType );
        daoUtil.setString( 2, strRoleKey );
        daoUtil.executeQuery(  );

        boolean bExists = false;

        while ( daoUtil.next(  ) )
        {
            bExists = true;
        }

        daoUtil.free(  );

        return bExists;
    }
}
